<?php

namespace App\Http\Controllers\Order;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Http\Validate\Order\SaleorderotherfeeSub as ValidateSaleorderotherfeeSub;
use App\Models\Order\Saleorder as ModelSaleorder;
use App\Models\Order\Saleorderfee as ModelSaleorderfee;
use App\Models\Order\SaleorderOtherfeePay;
use App\Models\Order\SaleorderOtherfeePayApprove;
use App\Services\Balance\Balance;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SaleorderOtherfeePayApproveController extends BaseController
{
    public function getinfo(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];

        $otherfee_id = intval($request->otherfee_id);

        $otherfeeinfo = SaleorderOtherfeePay::with(['tsinfo', 'cpinfo', 'pminfo', 'feeinfo','paybank','approve'])->where(['cpid' => $cpid, 'other_pid' => $otherfee_id])->first();

        if (!$otherfeeinfo) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }

        $list= SaleorderOtherfeePay::with(['tsinfo', 'cpinfo', 'pminfo', 'feeinfo','paybank','approve'])->where(['cpid' => $cpid, 'other_pid' => $otherfeeinfo->other_pid])
            ->where('pay_mid','<>',0)->orderBy('id','desc')->get();



        return response()->json(['code' => '000', 'data' => ['list' => $list]]);
    }

    public function docancel(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];

        $pay_id = intval($request->id);

        $SaleorderOtherfeePay = SaleorderOtherfeePay::where('id',$pay_id)->first();

        $SaleorderOtherfeeApprove = SaleorderOtherfeePayApprove::where(['cpid' => $cpid, 'otherfee_pay_id' => $pay_id])->first();


        if ($this->mangerinfo['utype'] != 7) {
            return response()->json(['code' => '001', 'msg' => '您无权限操作']);
        }
        if (!$SaleorderOtherfeePay) {
            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
        }
        if ($SaleorderOtherfeePay->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($SaleorderOtherfeeApprove->status > 2) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许撤审']);
        }
        $SaleorderOtherfeeApprove->status = 5;
        $SaleorderOtherfeeApprove->cs_at = time();
        $SaleorderOtherfeeApprove->save();

//        $data[] = [
//            'cpid' => $cpid,
//            'order_number'=>$otherfeeinfo->order_number,
//            'fee_type_id' => $otherfeeinfo->fee_type_id,
//            'fee_type_name' => $otherfeeinfo->fee_type_name,
//            'fee_date' => $otherfeeinfo->fee_date,
//            'fee_id' => $otherfeeinfo->fee_id,
//            'fee_name' => $otherfeeinfo->fee_name,
//            'company_name' => $otherfeeinfo->company_name,
//            'price' => $otherfeeinfo->price,
//            'remark' => $otherfeeinfo->remark,
//            'other_pid'=>$otherfeeinfo->other_pid,
//        ];
//        ModelSaleorderfee::insert($data);
        return response()->json(['code' => '000', 'msg' => '撤审成功']);
    }
    /****
     *  销售费用审核，
     *  统审1->2 通过，1->4 驳回
     *  操盘2->3 通过，2->4 驳回，重新插入了一条 其他费用单？
     *
     */
    public function changetstatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $pay_id = intval($request->id);

        $status = intval($request->status);
        $SaleorderOtherfeePay = SaleorderOtherfeePay::where('id',$pay_id)->first();
        $SaleorderOtherfeePayApprove = SaleorderOtherfeePayApprove::where(['cpid' => $cpid, 'otherfee_pay_id' => $pay_id])->first();

        //增加新的入口，所以订单确实未必存在
//        $SaleOrderFee = ModelSaleorderfee::where('id',$SaleorderOtherfeePay['other_pid'])->first();
//
//
//        if (!$SaleOrderFee) {
//            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
//        }
        if ($SaleorderOtherfeePay->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($SaleorderOtherfeePayApprove->status != 1) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许审核']);
        }
        if(!in_array($status,['2','4'])){
            return response()->json(['code' => '001', 'msg' => '审核状态异常']);
        }

        $SaleorderOtherfeePayApprove->status = $status;

        $SaleorderOtherfeePayApprove->ts_at = time();
        $SaleorderOtherfeePayApprove->save();

//        if($otherfeeinfo->status==4){
//            $data[] = [
//                'cpid' => $cpid,
//                'order_number'=>$otherfeeinfo->order_number,
//                'fee_type_id' => $otherfeeinfo->fee_type_id,
//                'fee_type_name' => $otherfeeinfo->fee_type_name,
//                'fee_date' => $otherfeeinfo->fee_date,
//                'fee_id' => $otherfeeinfo->fee_id,
//                'fee_name' => $otherfeeinfo->fee_name,
//                'company_name' => $otherfeeinfo->company_name,
//                'price' => $otherfeeinfo->price,
//                'remark' => $otherfeeinfo->remark,
//                'other_pid'=>$otherfeeinfo->other_pid,
//            ];
//            ModelSaleorderfee::insert($data);
//        }


        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    public function changecstatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $pay_id = intval($request->id);

        $SaleorderOtherfeePay = SaleorderOtherfeePay::where(['cpid' => $cpid, 'id' => $pay_id])->first();
        $SaleorderOtherfeePayApprove = SaleorderOtherfeePayApprove::where(['cpid' => $cpid, 'otherfee_pay_id' => $pay_id])->first();

        //增加新的入口，所以订单确实未必存在
//        $Saleorderfee = ModelSaleorderfee::where('id',$SaleorderOtherfeePay['other_pid'])->first();
//
//        if (!$Saleorderfee) {
//            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
//        }
        if ($SaleorderOtherfeePay->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($SaleorderOtherfeePayApprove->status != 2) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许审核']);
        }
        $SaleorderOtherfeePayApprove->status = 5;
        $SaleorderOtherfeePayApprove->cs_at = time();
        $SaleorderOtherfeePayApprove->save();

//        if($otherfeeinfo->status==4){
//            $data[] = [
//                'cpid' => $cpid,
//                'order_number'=>$otherfeeinfo->order_number,
//                'fee_type_id' => $otherfeeinfo->fee_type_id,
//                'fee_type_name' => $otherfeeinfo->fee_type_name,
//                'fee_date' => $otherfeeinfo->fee_date,
//                'fee_id' => $otherfeeinfo->fee_id,
//                'fee_name' => $otherfeeinfo->fee_name,
//                'company_name' => $otherfeeinfo->company_name,
//                'price' => $otherfeeinfo->price,
//                'remark' => $otherfeeinfo->remark,
//                'other_pid'=>$otherfeeinfo->other_pid,
//            ];
//            ModelSaleorderfee::insert($data);
//        }

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    public function changecptatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $pay_id = intval($request->id);//pay_id
        $status = intval($request->status);
        $SaleorderOtherfeePay = SaleorderOtherfeePay::where(['cpid' => $cpid, 'id' => $pay_id])->first();
        $SaleorderOtherfeePayApprove = SaleorderOtherfeePayApprove::where(['otherfee_pay_id'=>$pay_id,'cpid'=>$cpid])->first();

        //增加新的入口，所以订单确实未必存在
//        $Saleorderfee = ModelSaleorderfee::where('id',$SaleorderOtherfeePay['other_pid'])->first();
//        if (!$Saleorderfee) {
//            return response()->json(['code' => '001', 'msg' => '费用信息不存在']);
//        }
        if ($SaleorderOtherfeePay->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '费用未支付']);
        }
        if ($SaleorderOtherfeePayApprove->status != 2) {
            return response()->json(['code' => '001', 'msg' => '此状态不允许审核']);
        }
        if(!in_array($status,['3','4'])){
            return response()->json(['code' => '001', 'msg' => '审核状态异常']);
        }
        $SaleorderOtherfeePayApprove->status = $status;

        $SaleorderOtherfeePayApprove->cp_at = time();
        $SaleorderOtherfeePayApprove->save();

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    public function doreapay(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $req_data = $request->only([
            'id',
            'bank_name',
            'pay_money',
            'pay_at',
            'screenshot_file',
            'remark'
        ]);

        $Validate = new ValidateSaleorderotherfeeSub();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $info = SaleorderOtherfeePay::where(['cpid' => $cpid, 'is_del' => 1, 'id' => $req_data['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        $approveInfo = SaleorderOtherfeePayApprove::where(['cpid' => $cpid, 'is_del' => 1, 'otherfee_pay_id' => $req_data['id']])->first();
        if ($approveInfo->status != 3) {
            return response()->json(['code' => '001', 'msg' => '审核成功方可付款']);
        }

        //只允许办款一次
        $subInfo = DB::table('sale_order_otherfee_sub')->where(['pid' => $req_data['id']])->first();
        if ($subInfo) {
            return response()->json(['code' => '001', 'msg' => '此费用已付款']);
        }

        $settlement_price = $info->settlement_price;

        $data = [
            'pid' => $req_data['id'],
            'bank_name' => $req_data['bank_name'],
            'pay_money' => $req_data['pay_money'],
            'screenshot_file' => $req_data['screenshot_file'],
            'remark' => isset($req_data['remark']) ? $req_data['remark'] : '',
            'create_at' => time(),
        ];
        DB::table('sale_order_otherfee_sub')->insert($data);

        SaleorderOtherfeePay::where(['cpid' => $cpid, 'is_del' => 1, 'id' => $req_data['id']])->increment('pay_money_real', $data['pay_money']);

        //获取费用单的结算金额
        //修改往来单位的余额
        Balance::changeBalance( $info['company_id'],$cpid, $req_data['pay_money'], $settlement_price, $this->mangerinfo['cpid']);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

}
